// pages/commentDetail/commentDetail.js

var app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    isHiddenNum: 0,
    url: app.data.imgUrl,
    commentListParams: { //二级评论分页
      newsId: '', //新闻Id
      id: '', //一级评论Id
      page: 1,
      numPerPage: 10,
      sort: 1 //排序
    },
    isColor: true, //排序按钮字体颜色
    comment: null,
    secondaryComment: null,
    status: null,
    isReply: true,
    replyfocus: false,
    replyParams: {
      id: '',
      content: ''
    },
    isAdminLogin: false,
    adminUserInfo: null,
    userInfo: null,
    secondarySynchroData: { //数据同步用
      id:'',//评论id
      secondaryCommentNum: 0,
      dainzan: 0
    }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    //获取管理员登录状态
    this.data.isAdminLogin = wx.getStorageSync('teamLogin') //管理员登录状态
    //获取管理员信息
    this.data.adminUserInfo = wx.getStorageSync("adminUserInfo") //获取管理员信息
    //获取用户Id
    let accId = wx.getStorageSync('loginInfo').accId

    this.data.userInfo = this.getUserDetail(accId)
    //获取newid id
    this.data.commentListParams.id = options.id
    this.data.commentListParams.newsId = options.newsId
    this.data.replyParams.id = options.id//评论参数
    //获取该条评论
    if (app.globalData.commentlist != null) {
      this.data.comment = app.globalData.commentlist
      this.data.secondarySynchroData.id = this.data.comment.id
      wx.teamApi.news.commentlistTwo(this.data.commentListParams).then((res) => {
        this.setData({
          comment: this.data.comment,
          secondaryComment: res.data.result
        })
        // console.log('49')
        // console.log(this.data.comment)
      })
    }
  },
  //获取用户信息
  getUserDetail(id) {
    wx.teamApi.account.info(id).then(res => {
      this.setData({
        userInfo: res.data.result
      })
      console.log('70')
      console.log(this.data.userInfo)
    })
  },
  //二级评论点赞事件
  upvoteTwo(e) {
    let data = e.currentTarget.dataset.item
    let index = e.currentTarget.dataset.index
    if (data.upStatus) {
      this.cancelUpCommentTwo(data.id, index)
    } else {
      this.upCommentTwo(data.id, index)
    }
  },
  upCommentTwo(id, index) {
    wx.teamApi.news.upComment(id).then(res => {
      if (res.data.errcode === 0) {
        this.data.secondaryComment[index].upStatus = 1
        this.data.secondaryComment[index].upNum += 1
        this.setData({
          secondaryComment: this.data.secondaryComment
        })
      }
    })
  },
  cancelUpCommentTwo(id, index) {
    wx.teamApi.news.cancelUpComment(id).then(res => {
      if (res.data.errcode === 0) {
        this.data.secondaryComment[index].upStatus = 0
        this.data.secondaryComment[index].upNum -= 1
        this.setData({
          secondaryComment: this.data.secondaryComment
        })
      }
    })
  },
  //给楼主点赞事件
  upvote(e) {
    if (this.data.comment.upStatus) {
      this.cancelUpComment(this.data.comment.id)
    } else {
      this.upComment(this.data.comment.id)
    }
  },
  //添加
  upComment(id) {
    wx.teamApi.news.upComment(id).then(res => {
      if (res.data.errcode === 0) {
        this.data.comment.upStatus = 1
        this.data.comment.upNum += 1
        this.setData({
          comment: this.data.comment,
          'secondarySynchroData.dainzan': this.data.secondarySynchroData.dainzan + 1
        })
      }
    })
  },
  //取消
  cancelUpComment(id) {
    wx.teamApi.news.cancelUpComment(id).then(res => {
      if (res.data.errcode === 0) {
        this.data.comment.upStatus = 0
        this.data.comment.upNum -= 1
        this.setData({
          comment: this.data.comment,
          'secondarySynchroData.dainzan': this.data.secondarySynchroData.dainzan - 1
        })
      }
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
      console.log('156')
      console.log()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {
    
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {
    app.globalData.secondarySynchroData = this.data.secondarySynchroData
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  },
  //点击排序按钮事件，调出隐藏框
  sortType(e) {
    let hidenum = e.currentTarget.dataset.hide
    if (hidenum == 0) {
      this.setData({

        isHiddenNum: 1
      })
    } else {
      this.setData({

        isHiddenNum: 0
      })
    }
  },


  //排序事件
  sort(e) {
    let sortNum = e.currentTarget.dataset.sort
    //封装对象
    this.data.commentListParams.sort = sortNum
    this.data.commentListParams.page = 1

    if (sortNum == 2) { //时间
      //给按时间字体颜色
      this.setData({
        isColor: false,
        isNone: 'none',
        isHiddenNum: 0
      })

    } else { //热度
      //给按热点字体颜色
      this.setData({
        isColor: true,
        isNone: 'none',
        isHiddenNum: 0
      })
    }
    //调用接口
    wx.teamApi.news.commentlistTwo(this.data.commentListParams).then((res) => {
      this.setData({
        secondaryComment: res.data.result
      })
    })
  },

  //拉出评论事件
  comment() {
    if (this.data.comment.accBlacklist == false || this.data.isAdminLogin) {
      this.setData({
        status: 'replay',
        isReply: false,
        replyfocus: true
      })
    } else {
      wx.showToast({
        title: '已被该社团拉黑！不能进行评论哦！',
        icon: 'none'
      })
    }
  },
  //获取输入值
  replyContentInput(e) {
    this.setData({
      'replyParams.content': e.detail.value
    })
  },
  //失去焦点事件
  inputBlur() {
    this.setData({
      status: 'usual'
    })
  },
  //评论事件
  submitRepty() {
    wx.teamApi.news.replycomment(this.data.replyParams).then(res => { //回复成功后：返回这条评论的id
      if (res.data.errcode === 0) {
        //评论成功
        var comments = [res.data.result]
        let arr = comments.concat(this.data.secondaryComment)
        this.setData({
          'replyParams.content': '',
          secondaryComment: arr,
          'secondarySynchroData.secondaryCommentNum': this.data.secondarySynchroData.secondaryCommentNum + 1
        })
        wx.showToast({
          title: '评论成功！'
        })
      }
    })
  },
  //拉黑用户
  setBlacklist(e) {
    console.log(this.data.adminUserInfo)
    //[1]判断是否是管理员
    if (!!this.data.isAdminLogin && this.data.adminUserInfo.associationId == this.data.comment.associationId) {
      //【2】获取该评论用户ID
      let accId = e.currentTarget.dataset.info.accId //id
      //[3]判断被拉黑用户是否是自己
      if (e.currentTarget.dataset.info.isAssociation == 0) {
        wx.showModal({
          title: '提示',
          content: '是否拉黑该用户？',
          success: (res) => {
            if (res.confirm) { //用户点击确定
              //调用拉黑接口
              wx.teamApi.account.setBlacklist(accId, 0).then(res => {
                console.log('444')
                console.log(res)
                //成功设置
                this.data.isBlacklist = true
              })
            }
          }
        })
      } else {
        wx.showToast({
          title: '管理员账号不能被拉黑！',
          icon: 'none'
        })
      }
    }
  },
  //删除评论
  deleteComment(e) {
    //[2]判断是否是管理员
    if (this.data.isAdminLogin && this.data.adminUserInfo.associationId == this.data.comment.associationId) {
      //【3】获取该评论用户ID
      let accId = e.currentTarget.dataset.info.accId
      //[4]获取这条评论id
      let commentId = e.currentTarget.dataset.info.id
      //获取评论下标
      let temp = e.currentTarget.dataset.commentindex
      //判断当前评论用户是否是管理员
      if (true) { //不是才执行
        //删除评论
        wx.showModal({
          title: '提示',
          content: '是否删除该条评论',
          success: (res) => {
            if (res.confirm) { //确定
              //获取评论id
              let id = e.currentTarget.dataset.info.id
              //调用删除接口
              wx.teamApi.news.deletecomment(accId, commentId, this.data.comment.newsId).then(res => {
                if (res.data.errcode === 0) {
                  var arr = this.data.secondaryComment
                  arr.splice(temp, 1)
                  this.setData({
                    secondaryComment: arr,
                    'secondarySynchroData.secondaryCommentNum': this.data.secondarySynchroData.secondaryCommentNum - 1
                  })
                }
              })
            }
          }
        })
      }
    }
  },
})